package com.mediacloud.app.newsmodule.activity.microlive

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.util.Log
import android.webkit.WebSettings
import android.webkit.WebView
import com.mediacloud.app.model.fragment.BaseFragment
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.CatalogItem
import com.mediacloud.app.model.news.kt.TypeX
import com.mediacloud.app.model.utils.SDKNewsItemJump
import com.mediacloud.app.model.utils.WebViewUtils
import com.mediacloud.app.model.view.WebBrowser
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.microlive.Lives
import com.mediacloud.app.newsmodule.microlive.LivesType3
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.mediacloud.app.newsmodule.utils.WebViewLinkCMSNewsHandle

class MicroLiveWebViewFragment(var webContent: String? = null) : BaseFragment(), IMicroLiveX {


    lateinit var webview: WebBrowser
    override fun getLayoutResID(): Int {
        return R.layout.fragment_microlive_webfragment
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
    }



    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {
        webview = mRootView.findViewById(R.id.webview)
        webview.isNestedScrollingEnabled = true
        webview.settings.loadWithOverviewMode = true
        webview.settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        webview.settings.javaScriptEnabled = true
        webview.settings.setGeolocationEnabled(true)
        webview.settings.domStorageEnabled = true
        webview.setInitialScale(25)
        webview.settings.useWideViewPort = true
        webview.setWebViewClient(object : WebBrowser.WebClient(activity, webview) {
            var catalogItem = CatalogItem()
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                var url = url
                Log.w("XXX", "shouldOverrideUrlLoading$url")
                url = WebViewUtils.replaceDoubleQuesMark(url)
                if (!WebViewLinkCMSNewsHandle.handleCMSNews(url, activity, null, catalogItem) && !SDKNewsItemJump.isMoreFunSDK(activity, url)) {
                    val articleItem = ArticleItem()
                    articleItem.isComment = "0"
                    articleItem.content = "APPTAG"
                    articleItem.isLinkNews = false
                    articleItem.type = TypeX.NEWS.LINK
                    articleItem.url = url
                    //暂时不要标题
                    articleItem.title = "  "//catalogItem != null ? catalogItem.getCatname() : "");
                    val needToken = WebViewUtils.getNeedTokenTag(url)
                    val canCollect = false
                    val canShare = true
                    val intent = Intent()
                    intent.putExtra(Intent.EXTRA_REFERRER, 1)
                    NewsItemClickUtils.OpenItemNewsHandle(activity, articleItem.type, articleItem, catalogItem, intent, needToken, canCollect, canShare)
                }
                return true
            }
        })
        webContent?.let {
            webContent?.let {
                webview.loadUrl(it)
            }
        }
    }

    fun setContent(webContent: String?) {
        webContent?.let {
            webview.loadUrl(webContent)
        }
    }

    override fun onPause() {
        super.onPause()
        webview.onPause()
    }

    override fun onResume() {
        super.onResume()
        webview.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        webview.stopLoading()
        webview.destroy()
    }

    fun callOrderJS(order: Int) {
        webview.evaluateJavascript("javascript:webViewListSort($order)", null)
    }

}